package com.cgtong.android.widget;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.FragmentManager;
import android.view.View;

/**
 * Created by cc
 * on 2017/8/16.
 */
public class BaseDialog extends BaseDialogFragment {

  private static final String KEY_LAYOUT_RES = "dialog_layout_res";
  private static final String KEY_HEIGHT = "dialog_height";
  private static final String KEY_DIM = "dialog_dim";
  private static final String KEY_GRAVITY = "dialog_gravity";
  private static final String KEY_CANCEL_OUTSIDE = "dialog_cancel_outside";
  //---------------------
  private boolean mIsCancelOutSide = super.getCancelOutSide();
  private boolean mIsCancelable = super.getCanCancelable();
  private String mTag = super.getFragmentTag();
  private float mDimAmount = super.getDimAmount();
  private int mHeight = super.getHeight();
  private int mGravity = super.getGravity();
  private int mStyle = super.getStyle();

  //---------------------
  private FragmentManager mFragmentManager;

  @LayoutRes
  private int mLayoutRes;
  private ViewListener mViewListener;

  public BaseDialog() {
  }

  public static BaseDialog create(FragmentManager manager) {
    BaseDialog baseDialog = new BaseDialog();
    baseDialog.setFragmentManager(manager);
    return baseDialog;
  }

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
      this.mLayoutRes = savedInstanceState.getInt(KEY_LAYOUT_RES);
      this.mHeight = savedInstanceState.getInt(KEY_HEIGHT);
      this.mGravity = savedInstanceState.getInt(KEY_GRAVITY);
      this.mDimAmount = savedInstanceState.getFloat(KEY_DIM);
      this.mIsCancelOutSide = savedInstanceState.getBoolean(KEY_CANCEL_OUTSIDE);
    }
  }

  public void onSaveInstanceState(Bundle outState) {
    outState.putInt(KEY_LAYOUT_RES, this.mLayoutRes);
    outState.putInt(KEY_HEIGHT, this.mHeight);
    outState.putInt(KEY_GRAVITY, this.mGravity);
    outState.putFloat(KEY_DIM, this.mDimAmount);
    outState.putBoolean(KEY_CANCEL_OUTSIDE, this.mIsCancelOutSide);
    super.onSaveInstanceState(outState);
  }

  public void bindView(final View view, Dialog dialog) {
    if (this.mViewListener != null) {
      this.mViewListener.bindView(view, dialog);
    }
  }

  public BaseDialog setGravity(int gravity) {
    this.mGravity = gravity;
    return this;
  }

  public int getLayoutRes() {
    return this.mLayoutRes;
  }

  public BaseDialog setFragmentManager(FragmentManager manager) {
    this.mFragmentManager = manager;
    return this;
  }

  public BaseDialog setViewListener(ViewListener listener) {
    this.mViewListener = listener;
    return this;
  }

  public BaseDialog setLayoutRes(@LayoutRes int layoutRes) {
    this.mLayoutRes = layoutRes;
    return this;
  }

  public BaseDialog setCancelOutSide(boolean cancel) {
    this.mIsCancelOutSide = cancel;
    return this;
  }

  public BaseDialog setTag(String tag) {
    this.mTag = tag;
    return this;
  }

  public BaseDialog setDimAmount(float dim) {
    this.mDimAmount = dim;
    return this;
  }

  public BaseDialog setHeight(int heightPx) {
    this.mHeight = heightPx;
    return this;
  }


  public BaseDialog setStyle(int style) {
    this.mStyle = style;
    return this;
  }

  public BaseDialog setCanCancelable(boolean cancelable) {
    this.mIsCancelable = cancelable;
    return this;
  }

  @Override
  public boolean getCanCancelable() {
    return this.mIsCancelable;
  }

  @Override
  public int getStyle() {
    return this.mStyle;
  }

  @Override
  public int getGravity() {
    return this.mGravity;
  }

  @Override
  public float getDimAmount() {
    return this.mDimAmount;
  }

  @Override
  public int getHeight() {
    return this.mHeight;
  }

  @Override
  public boolean getCancelOutSide() {
    return this.mIsCancelOutSide;
  }

  @Override
  public String getFragmentTag() {
    return this.mTag;
  }

  public BaseDialogFragment show() {
    this.show(this.mFragmentManager, this.getFragmentTag());
    return this;
  }

  public interface ViewListener {

    void bindView(View view, Dialog dialog);
  }
}
